﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using Meqs.Utilities;
using Meqs.Logic.Interface;

public partial class School_ImportTeacher : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int opType = 0;
            string strOpType = Request.QueryString["optype"];
            if (strOpType == null)
            {
                Response.Write("<script>parent.showMassgeBox('系统提示','导入发生错误，请重新导入!',1,null);</script>");
                return;
            }
            else
            {
                try { opType = Convert.ToInt32(strOpType); }
                catch
                {
                     Response.Write("<script>parent.showMassgeBox('系统提示','导入发生错误，请重新导入!',1,null);</script>");
                    return;
                }
            }

            if (this.Request.Files.Count <= 0 || this.Request.Files[0].FileName == "")
            {
                Response.Write("<script>parent.showMassgeBox('系统提示','请选择Excel文件!',1,null);</script>");
                return;
            }

            DataSet ds = new DataSet();
            Stream st = this.Request.Files[0].InputStream;

            if (st == null)
            {
                Response.Write("<script>parent.showMassgeBox('系统提示','导入文件失败，请重新导入！',1,null);</script>");
                return;
            }

            ds = ExcelUtil.ExcelImport(st);

            if (ds == null)
            {
                Response.Write("<script>parent.showMassgeBox('系统提示','导入文件失败，请重新导入！',1,null);</script>");
                return;
            }

            if (ds.Tables.Count <= 0)
            {
                Response.Write("<script>parent.showMassgeBox('系统提示','导入文件数据格式不正确，请重新导入！',1,null);</script>");
                return;
            }

            //if (ds.Tables[0].TableName != "教师信息")
            //{
            //    Response.Write("<script>parent.showMassgeBox('系统提示','导入文件数据格式不正确，请重新导入！',1,null);</script>");
            //    return;
            //}

            if (ds.Tables[0].Columns.Count != 5 ||
                ds.Tables[0].Columns[0].ColumnName != "教师工号" ||
                ds.Tables[0].Columns[1].ColumnName != "教师姓名" ||
                ds.Tables[0].Columns[2].ColumnName != "性别" ||
                ds.Tables[0].Columns[3].ColumnName != "出生日期" ||
                ds.Tables[0].Columns[4].ColumnName != "入职日期")
            {
                Response.Write("<script>parent.showMassgeBox('系统提示','导入文件数据格式不正确，请重新导入！',1,null);</script>");
                return;
            }

            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>parent.showMassgeBox('系统提示','导入文件中无数据，填充数据后导入！',1,null);</script>");
                return;
            }

            //将Table中的性别字段设置为正确的格式
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (row[0].ToString().Trim() == "" || row[1].ToString().Trim() == "" || row[2].ToString().Trim() == "")
                    continue;

                //0列，学号
                //1列，姓名
                //2列，性别
                //3列，出生日期
                //4列，入学日期
                if (row[2].ToString().Trim() == "男")
                    row[2] = 0;
                else if (row[2].ToString().Trim() == "女")
                    row[2] = 1;
                else
                {
                    Response.Write("<script>parent.showMassgeBox('系统提示','性别信息不符合导入规范，请重新修改后导入！',1,null);</script>");
                    return;
                }
            }

            //这里调用逻辑层的导入函数
            int iRes = Container<ITeacherInfoLogic>.Instance.ImportTeacherInfo(Common.GetConstSchoolMark(), ds.Tables[0], opType);

            if (iRes == 0)
                Response.Write("<script>parent.showMassgeBox('系统提示','导入文件失败，请重新导入！',1,null);</script>");
            else
                Response.Write("<script>parent.showMassgeBox('系统提示','导入文件成功！',1,null);parent.CloseForm();parent.Load();</script>");
        }
    }
}
